package org.jeecg.modules.paycenter.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.paycenter.service.IMemberChannelService;
import org.jeecg.modules.paycenter.service.MemberChannelFactory;
import org.jeecg.modules.paycenter.service.WalletService;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.customer.entity.HyCustomerAccountBook;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerAccountBookService;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerService;
import org.jeecg.modules.wlhy.customerwallet.entity.HyCustomerWallet;
import org.jeecg.modules.wlhy.customerwallet.service.IHyCustomerWalletService;
import org.jeecg.modules.wlhy.hyconfig.entity.HyConfig;
import org.jeecg.modules.wlhy.utils.HyCfgUtil;
import org.jeecg.modules.wlhy.utils.HyConfigUtils;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.jeecg.modules.wlhy.walletcfg.entity.HyWalletCfg;
import org.jeecg.modules.wlhy.walletcfg.service.IHyWalletCfgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户钱包服务
 */
@RestController
@RequestMapping("/paycenter/wallet")
@Slf4j
public class HyWalletController extends JeecgController<HyWalletCfg, IHyWalletCfgService> {

    @Autowired
    private WalletService walletService;

    @Autowired
    private UserUtil userUtil;

    @Autowired
    private IHyCustomerAccountBookService hyCustomerAccountBookService;

    @Autowired
    private IHyCustomerWalletService hyCustomerWalletService;

    @Autowired
    private IHyCustomerService hyCustomerService;

    @ApiOperation(value = "钱包配置列表", notes = "钱包配置列表")
    @GetMapping(value = "/cfg")
    public Result<?> cfg() {
        HyConfig config = HyConfigUtils.getConfig();
        Map<String, Object> data = new HashMap<>();
        data.put("pa" , config.getPingAnIsUse() ? "1" : "0");
        data.put("zhengzhou" , config.getZhengzhouIsUse() ? "1" : "0");
        data.put("nhST", config.getNhbankIzUse() ? "1" : "0");
        data.put("sysST", "1");
        data.put("pfST", config.getPfIzUse() ? "1" : "0");
        data.put("tlST", HyCfgUtil.isTongLianUsed() ? "1" : "0");
        return Result.OK(data);
    }

    //region base
    @PostMapping(value = "/addpwd")
    public Result<?> addPwd(@RequestBody JSONObject jsonObject) {
        walletService.addPwd(jsonObject);
        return Result.OK("设置密码成功！");
    }

    //设置货主密码
    @PostMapping(value = "/setpwd")
    public Result<?> setPwd(@RequestBody JSONObject jsonObject) {
        walletService.setPwd(jsonObject);
        return Result.OK("修改成功！");
    }

    @PostMapping(value = "/resetpwd")
    public Result<?> resetPwd(@RequestBody JSONObject jsonObject) {
        walletService.resetPwd(jsonObject);
        return Result.OK("操作成功!");
    }

    //endregion

    //region 货主
    @ApiOperation(value = "获取货主多级账簿信息", notes = "获取货主多级账簿信息")
    @GetMapping(value = "/getCustomerWallets")
    public Result<?> getAccountBookInfo() {
        HyCustomer customer = userUtil.getCustomer();
        if (customer == null) {
            throw new JeecgBootException("未获取到货主信息");
        }
        Map data = walletService.getCustomerWallets(customer.getId());
        return Result.OK(data);
    }

    @ApiOperation(value = "获取货主农行钱包信息", notes = "获取货主农行钱包信息")
    @GetMapping(value = "/nh/customer/getAcccount")
    public Result<?> getCustomerNhAccountInfo() {
        HyCustomer customer = userUtil.getCustomer();
        if (customer == null) {
            throw new JeecgBootException("未获取到货主信息");
        }
        HyConfig config = HyConfigUtils.getConfig();
        if (!config.getNhbankIzUse()) {
            return Result.error("未开通服务");
        }

        HyCustomerAccountBook nhWallet = hyCustomerAccountBookService.getWalletInfo(customer.getId());
        if (nhWallet == null) {
            return Result.error("未开通钱包！");
        }
        return Result.OK(nhWallet);
    }

    @ApiOperation(value = "获取货主系统钱包信息", notes = "获取货主系统钱包信息")
    @GetMapping(value = "/sys/customer/getAcccount")
    public Result<?> getCustomerSysAccountInfo() {
        HyCustomer customer = userUtil.getCustomer();
        if (customer == null) {
            throw new JeecgBootException("未获取到货主信息");
        }
        //系统钱包
        HyCustomerWallet sysWallet = hyCustomerWalletService.getWalletInfo(customer.getId());
        if (sysWallet == null) {
            return Result.error("未开通钱包！");
        }
        return Result.OK(sysWallet);
    }

    //开通货主密码
    @PostMapping(value = "/customer/addpwd")
    @RequiresPermissions("customer:setpwd")
    public Result<?> addCustomerPwd(@RequestBody JSONObject jsonObject) {
        walletService.addPwd(jsonObject);
        return Result.OK("设置密码成功！");
    }

    //设置货主密码
    @PostMapping(value = "/customer/setpwd")
    @RequiresPermissions("customer:setpwd")
    public Result<?> setCustomerPwd(@RequestBody JSONObject jsonObject) {
        walletService.setPwd(jsonObject);
        return Result.OK("修改成功！");
    }

    @PostMapping(value = "/customer/resetpwd")
    @RequiresPermissions("customer:resetpwd")
    public Result<?> resetCustomerPwd(@RequestBody JSONObject jsonObject) {
        walletService.resetPwd(jsonObject);
        return Result.OK("操作成功!");
    }
    //endregion

    //region 账户

    @AutoLog("获取货主账户余额")
    @ApiOperation(value = "获取货主账户余额", notes = "获取货主多级账簿信息")
    @PostMapping(value = "/getCustomerAvailBal")
    public Result<?> getCustomerAvailBal(@RequestBody JSONObject jo) {
        //paySource 支付源类型 1账户余额 2平台垫付 3经纪人垫付 4账期
        String paySource = jo.getString("paySource");
        if(StringUtils.isEmpty(paySource)){
            return Result.error("支付源错误");
        }
        String payChannel = jo.getString("payChannel");
        IMemberChannelService memberChannel = MemberChannelFactory.getMemberChannel(payChannel);
        if(memberChannel == null){
            return Result.error("暂不支持");
        }

        HyCustomer customer = null;
        String isAgent = jo.getString("isAgent");
        if("1".equals(isAgent)){
           //经纪人查询货主账户余额
            String customerId = jo.getString("cid");
            if(StringUtils.isEmpty(customerId)){
                return Result.error("货主Id不能为空");
            }
            customer = hyCustomerService.getById(customerId);
            return memberChannel.getCustomerAvailBalStr(customer);
        }

        if("1".equals(paySource)){
            //货主账户余额
            if(userUtil.isSys()){
                String customerId = jo.getString("cid");
                if(StringUtils.isEmpty(customerId)){
                    return Result.error("货主Id不能为空");
                }
                customer = hyCustomerService.getById(customerId);
            }else {
                customer = userUtil.getCustomer();
            }
        }else if("2".equals(paySource)){
            //平台垫付
            if(!userUtil.isSys()){
                return Result.error("不是管理员用户，无权限查询");
            }
            //查询平台账户余额
            return memberChannel.getPlateformAvailBalStr();
        }else if("3".equals(paySource)){
            //经纪人垫付运费获取经纪人钱包余额
            customer = userUtil.getCustomer();
        }else {
            return Result.error("支付源错误");
        }
        if (customer == null) {
            return Result.error("货主信息不存在");
        }
        return memberChannel.getCustomerAvailBalStr(customer);
    }

    //endregion

}
